package com.ccgk.logistics.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ccgk.common.page.PaginationVo;
import com.ccgk.logistics.domain.Pack;
import com.ccgk.logistics.domain.PackExample;
import com.ccgk.logistics.domain.Port;
import com.ccgk.logistics.domain.PortExample;
import com.ccgk.logistics.persistence.PackMapper;
import com.ccgk.logistics.service.PackService;
@Service
public class PackServiceImpl implements PackService{
	
	@Autowired
	private PackMapper packMapper;

	@Override
	public Integer add(Pack pack) {
		return packMapper.insert(pack);
	}

	@Override
	public void batchDelete(Integer[] idIntArray) {
		for(Integer id:idIntArray){
			delete(id);
		}
	}

	@Override
	public Integer delete(int id) {
		return packMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Pack> findAll() {
		return packMapper.selectByExample(null);
	}

	@Override
	public PaginationVo<Pack> findList(int currentPage, int pageSize) {
		PackExample example = new PackExample();
		List<Pack> data = packMapper.selectByExample(example,new RowBounds((currentPage-1)*pageSize, pageSize));
		int recordCount = packMapper.countByExample(example);
		PaginationVo<Pack> result = new PaginationVo<Pack>(data, recordCount, pageSize, currentPage);
		return result;
	}

	@Override
	public Pack getById(int id) {
		return packMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer update(Pack pack) {
		return packMapper.updateByPrimaryKey(pack);
	}

	@Override
	public Pack getByName(String name) {
		PackExample example = new PackExample();
		example.createCriteria().andNameLike(name);
		List<Pack> result = packMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(result))
			return null;
		return result.get(0);
	}
	
	
}
